/*
 * Copyright (c) 2017. 深圳一七科技. All rights reserved.
 */

package com.yiqi.liebang.feature.order.presenter;

import android.text.TextUtils;

import com.suozhang.framework.utils.T;
import com.yiqi.liebang.entity.bo.CommentBo;
import com.yiqi.liebang.entity.bo.ExpertTopicBo;
import com.yiqi.liebang.entity.bo.TopicOrderInfoBo;
import com.yiqi.liebang.feature.order.contract.TopicInfoContract;
import com.yiqi.liebang.feature.order.dependencies.topicinfo.DaggerTopicInfoPresenterComponent;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * LIJUWEN
 *
 * @email yiyayiyayaoljw@gmail.com
 * @date 2018/7/11 9:31
 */
public class TopicInfoPresenter implements TopicInfoContract.Presenter {

    private TopicInfoContract.View mView;

    @Inject

    TopicInfoContract.Model mModel;


    public TopicInfoPresenter(TopicInfoContract.View mView) {
        this.mView = mView;
        DaggerTopicInfoPresenterComponent.create().inject(this);
    }

    @Override
    public void start() {

    }


    @Override
    public void getOrderQuestionInfo(String id, int type) {
        mModel.getOrderTopicInfo(id, type)
                .compose(mView.bindToLife())
                .subscribe(new Observer<TopicOrderInfoBo>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(TopicOrderInfoBo infofBo) {
                        mView.dismissLoading();
                        mView.showOrderTopicInfoSuccess(infofBo);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showOrderTopicInfoError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void refusedQuestion(String id) {
        mModel.refusedQuestion(id)
                .compose(mView.bindToLife())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(String infofBo) {
                        mView.dismissLoading();
                        mView.showRefusedQuestion(infofBo);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showOrderTopicInfoError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void toExpertConfrim(ExpertTopicBo expertTopicBo) {
        if (expertTopicBo == null) {
            return;
        }
        mModel.toExpertConfrim(expertTopicBo)
                .compose(mView.bindToLife())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(String s) {
                        mView.dismissLoading();
                        mView.showExpertConfrim(s);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showOrderTopicInfoError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void toStudentConfrim(String id) {
        if (TextUtils.isEmpty(id)) {
            return;
        }
        mModel.toStudentConfrim(id).compose(mView.bindToLife())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(String s) {
                        mView.dismissLoading();
                        mView.showStudentConfrim(s);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showOrderTopicInfoError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void toCommentTopicOrder(String commitUserUid, String comment, float score, String id) {
        if (score <= 0) {
            T.showShort("请选择评分！");
            return;
        }
        if (TextUtils.isEmpty(comment)) {
            T.showShort("请输入点评内容！");
            return;
        }
        mModel.commentTopicOrder(new CommentBo(commitUserUid,comment, score,id))
                .compose(mView.bindToLife())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(String infofBo) {
                        mView.dismissLoading();
                        mView.showCommentSuccess(infofBo);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showOrderTopicInfoError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }



    @Override
    public void cancleOrder(String id) {
        mModel.cancleOrder(new CommentBo(id))
                .compose(mView.bindToLife())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(String infofBo) {
                        mView.dismissLoading();
                        mView.showCancleSuccess(infofBo);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showOrderTopicInfoError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void toStudentComplete(String id) {
        if (TextUtils.isEmpty(id)) {
            return;
        }
        mModel.toStudentComplete(id).compose(mView.bindToLife())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(String s) {
                        mView.dismissLoading();
                        mView.showStudentComplete();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showOrderTopicInfoError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void toExpertComplete(String id) {
        if (TextUtils.isEmpty(id)) {
            return;
        }
        mModel.toExpertComplete(id).compose(mView.bindToLife())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(String s) {
                        mView.dismissLoading();
                        mView.showExpertComplete();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showOrderTopicInfoError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void remindExpertsConfirm(String id) {
        if (TextUtils.isEmpty(id)) {
            return;
        }
        mModel.remindExpertsConfirm(new ExpertTopicBo(id)).compose(mView.bindToLife())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(String s) {
                        mView.dismissLoading();
                        mView.showRemindExpertsSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showOrderTopicInfoError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }
}
